<?php if(!defined('DB_NAME')) die();

//SEO
function ini_seo(){
	global $post;
	if ( !is_home() ) {
		echo '<title>';
		if ( is_search() ){ echo $s.' 的搜索结果';}
		if ( is_singular() ){
			echo trim(wp_title('|&nbsp;',0,'right'));
				if (get_query_var('page')){ echo '-第'.get_query_var('page').'页';
			}
		echo get_bloginfo('name')."</title>\n";
		}
		else {
			if ( is_category() ){ single_cat_title(); }
			if ( is_year() ){ the_time('Y年发表的文章'); }
			if ( is_month() ){ the_time('Y年n月发表的文章'); }
			if ( is_day() ){ the_time('Y年n月j日发表的文章'); }
			if ( is_author() ){ echo get_the_author('nickname').'发表的文章'; }
			if ( function_exists('is_tag') && is_tag() ){ single_tag_title("", true); }
			echo ' | '.get_bloginfo('name')."</title>\n";
		}
	}
	else {
		echo "<title>".get_bloginfo('name')." | ".get_bloginfo('description')."</title>\n";
	}

	$description = get('ini_description'); # 默认使用后台设置描述
	$keywords = get('ini_keywords'); # 默认使用后台设置关键词
	$tags = wp_get_post_tags($post->ID);

	if ( !is_home() ) {
		if (is_category()){
			if( category_description()!=='' ){
				$description = category_description();
				$description = str_replace("<p>","",str_replace("</p>","",$description)); # 分类页首选分类描述第一段
			}
			$keywords = single_cat_title('', false); # 分类关键词使用分类名称
		}
		elseif (have_posts()){
			if ($post->post_excerpt){
				$description  = $post->post_excerpt; # 文章页首选文章摘要作为描述
			} else
			if (get_post_meta($post->ID,'description',true)){
				$description = get_post_meta($post->ID,'description',true); # 次选描述元数据
			} else {
				if( preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>") ), $result) ){
					$post_content = $result['1'];
				} else {
					$post_content_r = explode( "\n", trim(strip_tags($post->post_content)) );
					$post_content = $post_content_r['0'];
				}
					$judge = utf8_str($post_content, 0, 180); # 截取文章首段落或者首句话，180字
				if ( ($judge)!=='' ) { $description = $judge; }
			}
			if ( get_post_meta($post->ID,'keywords',true) ){ # 文章页首选关键词元数据
				$keywords = get_post_meta($post->ID,'keywords',true);
			}
			elseif ( $tags!=='' ){ # 次选标签作为关键词
				foreach ($tags as $tag ){
					$tag_keywords = $tag_keywords .$tag->name .",";
				}
				$keywords = $tag_keywords;
			}
		}
	}
	echo "<meta name=\"description\" content=\"".trim($description)."\" />\n<meta name=\"keywords\" content=\"".rtrim($keywords,',')."\" />\n";
}

//面包屑导航
function ini_map() {
	$delimiter = ' » '; //分隔符
	if ( !is_home() && !is_front_page() || is_paged() ) {
		echo '<div itemscope itemtype="http://schema.org/WebPage" id="map">';
		global $post;
		$homeLink = home_url();
		echo ' <a itemprop="breadcrumb" href="'.$homeLink.'"><i class="icon-home"></i> 主页</a>'.$delimiter;
		if ( is_category() ) { //分类 存档
			global $wp_query;
			$cat_obj = $wp_query->get_queried_object();
			$thisCat = $cat_obj->term_id;
			$thisCat = get_category($thisCat);
			$parentCat = get_category($thisCat->parent);
			if ($thisCat->parent != 0){
				$cat_code = get_category_parents($parentCat, TRUE, $delimiter);
				echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code );
			}
			echo single_cat_title('', false);
		} elseif ( is_day() ) { //天 存档
			echo '<a itemprop="breadcrumb" href="'.get_year_link(get_the_time('Y')).'">'.get_the_time('Y').'年</a>'.$delimiter;
			echo '<a itemprop="breadcrumb"  href="'.get_month_link(get_the_time('Y'),get_the_time('m')).'">'.get_the_time('n').'月</a>'.$delimiter;
			echo get_the_time('j').'日';
		} elseif ( is_month() ) { //月 存档
			echo '<a itemprop="breadcrumb" href="'.get_year_link(get_the_time('Y')).'">'.get_the_time('Y').'年</a>'.$delimiter;
			echo get_the_time('n').'月';
		} elseif ( is_year() ) { //年 存档
			echo get_the_time('Y').'年';
		} elseif ( is_single() && !is_attachment() ) { //文章
			if ( get_post_type() != 'post' ) { //自定义文章类型
				$post_type = get_post_type_object(get_post_type());
				$slug = $post_type->rewrite;
				echo '<a itemprop="breadcrumb" href="'.$homeLink.'/'.$slug['slug'].'/">'.$post_type->labels->singular_name.'</a>'.$delimiter.'正文';
			} else { //文章 post
				$cat = get_the_category(); $cat = $cat[0];
				$cat_code = get_category_parents($cat, TRUE, $delimiter);
				echo $cat_code = str_replace ('<a','<a itemprop="breadcrumb"', $cat_code ).'正文';
			}
		} elseif ( !is_single() && !is_page() && get_post_type() != 'post' ) {
			$post_type = get_post_type_object(get_post_type());
			echo $post_type->labels->singular_name;
		} elseif ( is_attachment() ) { //附件
			$parent = get_post($post->post_parent);
			$cat = get_the_category($parent->ID); $cat = $cat[0];
			echo '<a itemprop="breadcrumb" href="'.get_permalink($parent).'">'.$parent->post_title.'</a>'.$delimiter;
			echo get_the_title();
		} elseif ( is_page() && !$post->post_parent ) { //页面
			echo get_the_title();
		} elseif ( is_page() && $post->post_parent ) { //父级页面
			$parent_id  = $post->post_parent;
			$breadcrumbs = array();
			while ($parent_id) {
				$page = get_page($parent_id);
				$breadcrumbs[] = '<a itemprop="breadcrumb" href="'.get_permalink($page->ID).'">'.get_the_title($page->ID).'</a>';
				$parent_id  = $page->post_parent;
			}
			$breadcrumbs = array_reverse($breadcrumbs);
			foreach ($breadcrumbs as $crumb) echo $crumb.$delimiter;
			echo get_the_title();
		} elseif ( is_search() ) { //搜索结果
			printf( '%s 的搜索结果', get_search_query() );
		} elseif ( is_tag() ) { //标签 存档
			printf( '标签: %s', single_tag_title( '', false ) );
		} elseif ( is_author() ) { //作者存档
			global $author;
			$userdata = get_userdata($author);
			printf('%s 发布的文章', $userdata->display_name );
		} elseif ( is_404() ) { //404 页面
			echo '404';
		}
		if ( get_query_var('paged') ) { //分页
			echo sprintf( ' (第%s页)', get_query_var('paged') );
		}
		echo "</div>\n";
	}
}